<template>
  <a-popconfirm
      :title="title"
      @confirm="handleSubmit(ids)"
      okText="是"
      cancelText="否"
  >
    <a href="#">{{ stateMap[state] }}</a>
  </a-popconfirm>
</template>

<script>
import {updateStateUser} from '@/api/basic/user'

export default {
  props: {
    ids: {
      type: String,
      required: true,
    },
    state: {
      type: Number,
      required: true,
    },
  },
  data () {
    return {
      stateMap: {
        0: '重置',
        1: '停用',
        2: '注销',
      },
      title: ''
    }
  },
  created () {
    this.title = '确定要' + this.stateMap[this.state] + '这' + this.ids.split(',').length + '名用户吗？'
  },
  watch: {
    ids: (val) => {
      this.title = '确定要' + this.stateMap[this.state] + '这' + val.split(',').length + '名用户吗？'
    }
  },
  methods: {
    handleSubmit (ids) {
      updateStateUser({ids: ids, state: this.state})
          .then(() => {
            this.$emit('ok')
          }).catch(err => {
        this.$message.error(err.result || '操作失败！')
      })
    }
  }
}
</script>
